<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="赛事名称" prop="competitionName">
        <el-input
          v-model="queryParams.competitionName"
          placeholder="请输入赛事名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赛事是否结束" prop="isFinished">
        <el-select v-model="queryParams.isFinished" placeholder="请选择赛事是否结束" clearable>
          <el-option
            v-for="dict in dict.type.rac_finished_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="赛事详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入赛事详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否发布" prop="isPublished">
        <el-select v-model="queryParams.isPublished" placeholder="请选择是否发布" clearable>
          <el-option
            v-for="dict in dict.type.rac_published_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['busi:competition:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['busi:competition:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['busi:competition:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['busi:competition:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="competitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="赛事名称" align="center" prop="competitionName" />
      <el-table-column label="报名结束时间" align="center" prop="registrationEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赛事Banner图片URL" align="center" prop="bannerUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bannerUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="比赛开始时间" align="center" prop="raceStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.raceStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赛事是否结束" align="center" prop="isFinished">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rac_finished_type" :value="scope.row.isFinished"/>
        </template>
      </el-table-column>
      <el-table-column label="赛事介绍" align="center" prop="introduction" />
      <el-table-column label="赛事详细地址" align="center" prop="address" />
      <el-table-column label="是否发布" align="center" prop="isPublished">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rac_published_type" :value="scope.row.isPublished"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="关联赛事方案ID" align="center" prop="raceSchemaId" />-->
      <el-table-column label="赛事方案名称" align="center" prop="raceSchema.schemaName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePublish(scope.row)"
            v-hasPermi="['busi:competition:edit']"
          >发布</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRaceSchemaUpdate(scope.row)"
            v-hasPermi="['busi:competition:edit']"
          >绑定赛事方案</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['busi:competition:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busi:competition:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="赛事名称" prop="competitionName">
          <el-input v-model="form.competitionName" placeholder="请输入赛事名称" />
        </el-form-item>
        <el-form-item label="报名开始时间" prop="registrationStartTime">
          <el-date-picker clearable
            v-model="form.registrationStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="registrationEndTime">
          <el-date-picker clearable
            v-model="form.registrationEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="赛事Banner图片URL" prop="bannerUrl">
          <image-upload v-model="form.bannerUrl"/>
        </el-form-item>
        <el-form-item label="比赛开始时间" prop="raceStartTime">
          <el-date-picker clearable
            v-model="form.raceStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择比赛开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计比赛结束时间" prop="expectedEndTime">
          <el-date-picker clearable
            v-model="form.expectedEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计比赛结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="赛事是否结束" prop="isFinished">
          <el-select v-model="form.isFinished" placeholder="请选择赛事是否结束">
            <el-option
              v-for="dict in dict.type.rac_finished_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="赛事介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="赛事地点经度" prop="locationLongitude">
          <el-input v-model="form.locationLongitude" placeholder="请输入赛事地点经度" />
        </el-form-item>
        <el-form-item label="赛事地点纬度" prop="locationLatitude">
          <el-input v-model="form.locationLatitude" placeholder="请输入赛事地点纬度" />
        </el-form-item>
        <el-form-item label="赛事详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入赛事详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改赛事方案对话框 -->
    <el-dialog :title="title" :visible.sync="openRaceCompetition" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="赛事名称" prop="competitionName">
          <el-input v-model="form.competitionName" placeholder="请输入赛事名称" disabled/>
        </el-form-item>
        <el-form-item label="赛事方案" prop="raceSchemaId">
          <el-select v-model="form.raceSchemaId" placeholder="请选择赛事方案" >
            <el-option
              v-for="item in schemaList"
              :key="item.id"
              :label="item.schemaName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSchemaForm">确 定</el-button>
        <el-button @click="cancelSchema">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompetition,
  getCompetition,
  delCompetition,
  addCompetition,
  updateCompetition,
  publishCompetition
} from "@/api/busi/competition";
import {listSchema} from "@/api/busi/schema";

export default {
  name: "Competition",
  dicts: ['rac_published_type', 'rac_finished_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赛事管理表格数据
      competitionList: [],
      // 赛事方案列表
      schemaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示绑定赛事方案弹出层
      openRaceCompetition: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        competitionName: null,
        isFinished: null,
        address: null,
        isPublished: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        competitionName: [
          { required: true, message: "赛事名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSchemaList();
  },
  methods: {
    /** 查询赛事管理列表 */
    getList() {
      this.loading = true;
      listCompetition(this.queryParams).then(response => {
        this.competitionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询赛事方案列表 */
    getSchemaList() {
      this.loading = true;
      listSchema(null).then(response => {
        this.schemaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消赛事绑定按钮按钮
    cancelSchema() {
      this.openRaceCompetition = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        competitionName: null,
        registrationStartTime: null,
        registrationEndTime: null,
        bannerUrl: null,
        raceStartTime: null,
        expectedEndTime: null,
        isFinished: null,
        introduction: null,
        locationLongitude: null,
        locationLatitude: null,
        address: null,
        isPublished: null,
        sortWeight: null,
        raceSchemaId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCompetition(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赛事管理";
      });
    },
    /** 发布按钮操作 */
    handlePublish(row) {
      this.reset();
      const id = row.id
      publishCompetition(id).then(response => {
        this.form = response.data;
        if(this.form.raceSchemaId != null)
          this.$modal.msgSuccess("发布成功");
          this.getList(); //更新列表信息
      });
    },
    /** 绑定赛事方案按钮操作 */
    handleRaceSchemaUpdate(row) {
      this.reset();
      const id = row.id
      getCompetition(id).then(response => {
        this.form = response.data;
        this.openRaceCompetition = true;
        this.title = "绑定赛事方案";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompetition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompetition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交赛事绑定按钮 */
    submitSchemaForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompetition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openRaceCompetition = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除赛事管理编号为"' + ids + '"的数据项？').then(function() {
        return delCompetition(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('busi/competition/export', {
        ...this.queryParams
      }, `competition_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
